package com.imessage.action.base.product;


import com.bean.BaseConfig;
import com.bean.BaseProduct;
import com.bean.BusinessApple;
import com.imessage.action.BaseAction;
import com.imessage.type.BaseConfigCode;
import com.imessage.type.BaseState;
import com.imessage.type.BusinessAppleState;
import com.imessage.utils.DateUtil;
import com.imessage.utils.StrUtil;
import com.imessage.vo.ConfigVo;
import com.imessage.vo.ProductVo;
import io.ebean.Expr;
import io.ebean.Expression;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Slf4j
public class ProductBaseAction extends BaseAction {

    protected final String datagrid = "datagrid-baseProduct-filter";

    protected void setExpressionList(List<Expression> expressionList, ProductVo productVo) {
        if (StringUtils.isNotBlank(productVo.getName())) {
            expressionList.add(Expr.contains("name", productVo.getName()));
        }
        if (StringUtils.isNotBlank(productVo.getState())) {
            expressionList.add(Expr.eq("state", productVo.getState()));
        }
    }


    protected void setModelAndView(ModelAndView modelAndView, BaseProduct baseProduct) {
        modelAndView.addObject("baseProductForm", baseProduct);
        modelAndView.addObject("statusList", BaseState.values());
    }

    protected void setBaseProduct(ProductVo productVo, BaseProduct baseProduct) {
        copyProperties(productVo, baseProduct);
        if (StringUtils.isNotBlank(productVo.getState())) {
            baseProduct.setState(BaseState.fromValue(productVo.getState()));
        }
    }



    protected boolean saveExcel(String filePath) {
        try {
            File file = new File(filePath);
            InputStream is = new FileInputStream(file);
            String fileName = file.getName();
            Workbook hssfWorkbook = null;
            if (fileName.endsWith("xlsx")) {
                hssfWorkbook = new XSSFWorkbook(is);    //Excel 2007
            } else if (fileName.endsWith("xls")) {
                hssfWorkbook = new HSSFWorkbook(is);    //Excel 2003
            }
            // 循环工作表Sheet
            for (int numSheet = 0; numSheet < hssfWorkbook.getNumberOfSheets(); numSheet++) {
                Sheet hssfSheet = hssfWorkbook.getSheetAt(numSheet);
                if (hssfSheet == null) {
                    continue;
                }
                List<String> headlist = new ArrayList<String>();
                Row row0 = hssfSheet.getRow(0);// 第一张sheet的第一行
                if (row0 == null) {
                    continue;
                }
                for (int cellNum = 0; cellNum < row0.getLastCellNum(); cellNum++) {
                    Cell cell = row0.getCell(cellNum);
                    headlist.add(cell.getStringCellValue().trim());// 读取
                }
                // 循环行Row
                for (int rowNum = 1; rowNum <= hssfSheet.getLastRowNum(); rowNum++) {
                    Row row = hssfSheet.getRow(rowNum);
                    if (row == null) {
                        continue;
                    }
                    BaseProduct baseProduct = new BaseProduct();
                    for (int j = 0; j < headlist.size(); j++) { // 对每个单元格进行循环
                        String value = headlist.get(j);// 读取当前单元格的值
                        Cell cell = row.getCell(j);
                        if (cell == null || StringUtils.isBlank(cell.getStringCellValue())) {
                            continue;
                        }
                        String content = cell.getStringCellValue().trim();
                        if (value.toLowerCase().indexOf("name") != -1) {
                            baseProduct.setName(content);
                        }
                        if (value.toLowerCase().indexOf("family") != -1) {
                            baseProduct.setFamily(content);
                        }
                        if (value.toLowerCase().indexOf("manufacturer") != -1) {
                            baseProduct.setManufacturer(content);
                        }
                        if (value.toLowerCase().indexOf("biosversion") != -1) {
                            baseProduct.setBiosVersion(content);
                        }
                        if (value.toLowerCase().indexOf("biosdate") != -1) {
                            baseProduct.setBiosDate(content);
                        }
                        if (value.toLowerCase().indexOf("biosvendor") != -1) {
                            baseProduct.setBiosVendor(content);
                        }
                        if (value.toLowerCase().indexOf("systemversion") != -1) {
                            baseProduct.setSystemVersion(content);
                        }
                        if (value.toLowerCase().indexOf("chassismanufacturer") != -1) {
                            baseProduct.setChassisManufacturer(content);
                        }
                        if (value.toLowerCase().indexOf("locationinchassis") != -1) {
                            baseProduct.setLocationInChassis(content);
                        }
                        if (value.toLowerCase().indexOf("chassisassettag") != -1) {
                            baseProduct.setChassisAssetTag(content);
                        }
                        if (value.toLowerCase().indexOf("chassistype") != -1) {
                            baseProduct.setChassisType(content);
                        }
                        if (value.toLowerCase().indexOf("boardtype") != -1) {
                            baseProduct.setBoardType(content);
                        }
                        if (value.toLowerCase().indexOf("remark") != -1) {
                            baseProduct.setRemark(content);
                        }
                    }
                    BaseProduct product = baseService.findObject(BaseProduct.class, Expr.eq("name", baseProduct.getName()));
                    if (product == null) {
                        baseService.saveObject(baseProduct);
                    }
                }
            }
        } catch (Exception e) {
            log.error("", e);
            return false;
        }
        return true;
    }

}
